/**********************************************************
 * JTest.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 11:02:56 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.job;

import static com.printeverywhere.util.LoggerConstants.USERS_COUNT_EXCEPTION;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.jhlabs.image.DitherFilter;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.dto.SystemMaintenance;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class JTest.
 */
public class PrunePendingUserJob implements Job {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(PrunePendingUserJob.class);

	private SystemMaintenance systemMaintenance;
	/* (non-Javadoc)
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
	 */
	public void execute(JobExecutionContext context) throws JobExecutionException {
		if (logger.isDebugEnabled()) {
			logger.debug("execute(JobExecutionContext) - start"); //$NON-NLS-1$
		}
		systemMaintenance = (SystemMaintenance) CommonFunctionDelegator.getAllfromtable(SystemMaintenance.class.getName()).get(AdminConstants.ZERO_NUMBER);
		int daysToSubtract = (systemMaintenance.getPendingUsers() * -1);
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Expression.le("createdDate", DateUtil.addDate(DateUtil.getTodaysDate(), Calendar.DATE, daysToSubtract)));
		criteria.add(Expression.eq("active", false));
		criteria.add(Expression.isNotNull("activationKey"));
		Transaction transaction = session.beginTransaction();
		List<User> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			logger.error(USERS_COUNT_EXCEPTION);
			throw e;
		} finally {
			if (logger.isInfoEnabled()) {
				logger.info(AttributeConstants.CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}

		if(list != null){
			for (Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
				User user = (User) iterator.next();
				CommonFunctionDelegator.GeneralDelete(user);
			}
			logger.info("===> " + list.size());
		} else{
			logger.info("No record found...");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute(JobExecutionContext) - end"); //$NON-NLS-1$
		}
	}
	public static void main(String[] args) {
		SystemMaintenance systemMaintenance = (SystemMaintenance) CommonFunctionDelegator.getAllfromtable(SystemMaintenance.class.getName()).get(AdminConstants.ZERO_NUMBER);
		int daysToSubtract = (systemMaintenance.getPendingUsers() * -1);
		System.out.println(DateUtil.addDate(DateUtil.getTodaysDate(), Calendar.DATE, daysToSubtract));
	}
}